/*图片懒加载*/
Vue.use(Vue.lazyimg, {
	/**
	 * 是否开启淡入效果的全局选项
	 */
	fadein: true,
	/**
	 * 是否忽略横向懒加载的全局选项
	 */
	nohori: true,
	/**
	 * 对屏幕滚动的速度的阈值，滚动速度高于此值时，不加载图片
	 */
	speed: 20
})


new Vue({
	el: "body",
	data: {
		loading:false,
		kc: true,
		ts: false,
		yg: false,
		tp: false,
		gg: false,
		blackshow: false,
		flshow: false,
		pxshow: false,
		sxshow: false,
		ownPoint: "",
		shop: [],
		shopstar: [],
		shopattr1: [],
		shopattr2: [],
		b2cFar: [],
		newshop: false,
		ontime: false,
		mokeysend: false
	},
	created: function() {
		this.leancloudInit();
		this.baiduInit();
	},
	watch: {
		ownPoint: function() {
			var classify = geturl_ch(name);
			this.howFar(classify);
		}
	},
	methods: {
		back:function(){
			history.back();
		},
		leancloudInit: function() {
			var APP_ID = 'c4w6bVvWfh8xxcmkBE1G0gIt-gzGzoHsz';
			var APP_KEY = 'JBikg4Ksk76zcTi5BKYWvTJF';
			AV.init({
				appId: APP_ID,
				appKey: APP_KEY
			});
		},
		baiduInit: function() {
			var _this = this;
			var map = new BMap.Map("allmap"); // 创建Map实例
			var zbX = geturl_ch("zbx");
			var zbY = geturl_ch("zby");
			var nowPoint = new BMap.Point(zbX, zbY);
			_this.ownPoint = nowPoint;
		},
		howFar: function(classify) {
			this.loading=false;
			this.shop = [];
			this.shopstar = [];
			this.shopattr1 = [];
			this.shopattr2 = [];
			this.b2cFar = [];
			//根据距离筛选商家
			var _this = this;
			var juli;
			// 创建地址解析器实例
			var map = new BMap.Map("allmap");
			var myGeo = new BMap.Geocoder();
			// 将地址解析结果显示在地图上,并调整地图视野
			var query = new AV.Query('ELM_shop');
			query.contains('classify', '' + classify + '');
			query.find().then(function(data) {
				for(var i = 0; i < data.length; i++) {
					//使用闭包将i存入
					(function(i) {
						myGeo.getPoint(data[i].attributes.adress, function(point) {
							if(point) {
								/*商家和我现在的距离*/
								juli = map.getDistance(_this.ownPoint, point).toFixed(2);
								if(juli < 50000) {
									_this.shop.push(data[i]);
									_this.shopstar.push(parseInt(data[i].attributes.grade));
									_this.shopattr1.push(eval(data[i].attributes.attr1));
									_this.shopattr2.push(eval(data[i].attributes.attr2));
									_this.b2cFar.push((juli / 1000).toFixed(2));
								}
							} else {
								alert("请您输入正确格式(省+市+区+具体位置)");
							}
						})
					})(i);
				}
			}, function(error) {
				// 异常处理
			}).then(function(){
				_this.loading=true;
			});
			this.flshow = false;
			this.blackshow = false;
			this.pxshow = false;
			this.sxshow = false;
		},
		tz: function(classify) {
			location.href = "../shoplist/shoplist.html?classify=" + seturl(classify) + "&zbx=" + this.ownPoint.lng + "&zby=" + this.ownPoint.lat;
		},
		fl: function() {
			this.flshow = !this.flshow;
			this.pxshow = false;
			this.sxshow = false;
			this.blackshowfn();
		},
		px: function() {
			this.pxshow = !this.pxshow;
			this.flshow = false;
			this.sxshow = false;
			this.blackshowfn();
		},
		sx: function() {
			this.sxshow = !this.sxshow;
			this.pxshow = false;
			this.flshow = false;
			this.blackshowfn();
		},
		blackshowfn: function() {
			if((this.flshow == false) && (this.pxshow == false) && (this.sxshow == false)) {
				this.blackshow = false;
			} else {
				this.blackshow = true;
			}
		},
		blackDisplay: function() {
			this.flshow = false;
			this.blackshow = false;
			this.pxshow == false;
			this.sxshow == false;
		},
		qbsj: function(classify, e) {
			$(".shoplist_class_list_left>li").each(function(i) {
				$(this).attr("class", "");
			})
			e.currentTarget.className = "whitebgc";
			this.howFar(classify);
			this.flshow = false;
			this.blackshow = false;
		},
		kcClick: function(e) {
			$(".shoplist_class_list_left>li").each(function(i) {
				$(this).attr("class", "");
			})
			e.currentTarget.className = "whitebgc";
			this.kc = true;
			this.ts = false;
			this.yg = false;
			this.tp = false;
			this.gg = false;
		},
		tsClick: function(e) {
			$(".shoplist_class_list_left>li").each(function(i) {
				$(this).attr("class", "");
			})
			e.currentTarget.className = "whitebgc";
			this.kc = false
			this.ts = true;
			this.yg = false;
			this.tp = false;
			this.gg = false;
		},
		ygClick: function(e) {
			$(".shoplist_class_list_left>li").each(function(i) {
				$(this).attr("class", "");
			})
			e.currentTarget.className = "whitebgc";
			this.kc = false
			this.ts = false;
			this.yg = true;
			this.tp = false;
			this.gg = false;
		},
		tpClick: function(e) {
			$(".shoplist_class_list_left>li").each(function(i) {
				$(this).attr("class", "");
			})
			e.currentTarget.className = "whitebgc";
			this.kc = false
			this.ts = false;
			this.yg = false;
			this.tp = true;
			this.gg = false;
		},
		ggClick: function(e) {
			$(".shoplist_class_list_left>li").each(function(i) {
				$(this).attr("class", "");
			})
			e.currentTarget.className = "whitebgc";
			this.kc = false
			this.ts = false;
			this.yg = false;
			this.tp = false;
			this.gg = true;
		},
		xlpx: function() {
			this.shop = [];
			this.shopstar = [];
			this.shopattr1 = [];
			this.shopattr2 = [];
			this.b2cFar = [];
			//根据距离筛选商家
			var _this = this;
			var juli;
			// 创建地址解析器实例
			var map = new BMap.Map("allmap");
			var myGeo = new BMap.Geocoder();
			// 将地址解析结果显示在地图上,并调整地图视野
			var query = new AV.Query('ELM_shop');
			query.addDescending('salenum');
			query.find().then(function(data) {
				for(var i = 0; i < data.length; i++) {
					//使用闭包将i存入
					(function(i) {
						myGeo.getPoint(data[i].attributes.adress, function(point) {
							if(point) {
								/*商家和我现在的距离*/
								juli = map.getDistance(_this.ownPoint, point).toFixed(2);
								if(juli < 50000) {
									_this.shop.push(data[i]);
									_this.shopstar.push(parseInt(data[i].attributes.grade));
									_this.shopattr1.push(eval(data[i].attributes.attr1));
									_this.shopattr2.push(eval(data[i].attributes.attr2));
									_this.b2cFar.push((juli / 1000).toFixed(2));
								}
							} else {
								alert("请您输入正确格式(省+市+区+具体位置)");
							}
						})
					})(i);
				}
			}, function(error) {
				// 异常处理
			});
			this.flshow = false;
			this.blackshow = false;
			this.pxshow = false;
			this.sxshow = false;
		},
		pfpx: function() {
			this.shop = [];
			this.shopstar = [];
			this.shopattr1 = [];
			this.shopattr2 = [];
			this.b2cFar = [];
			//根据距离筛选商家
			var _this = this;
			var juli;
			// 创建地址解析器实例
			var map = new BMap.Map("allmap");
			var myGeo = new BMap.Geocoder();
			// 将地址解析结果显示在地图上,并调整地图视野
			var query = new AV.Query('ELM_shop');
			query.addDescending('grade');
			query.find().then(function(data) {
				for(var i = 0; i < data.length; i++) {
					//使用闭包将i存入
					(function(i) {
						myGeo.getPoint(data[i].attributes.adress, function(point) {
							if(point) {
								/*商家和我现在的距离*/
								juli = map.getDistance(_this.ownPoint, point).toFixed(2);
								if(juli < 50000) {
									_this.shop.push(data[i]);
									_this.shopstar.push(parseInt(data[i].attributes.grade));
									_this.shopattr1.push(eval(data[i].attributes.attr1));
									_this.shopattr2.push(eval(data[i].attributes.attr2));
									_this.b2cFar.push((juli / 1000).toFixed(2));
								}
							} else {
								alert("请您输入正确格式(省+市+区+具体位置)");
							}
						})
					})(i);
				}
			}, function(error) {
				// 异常处理
			});
			this.flshow = false;
			this.blackshow = false;
			this.pxshow = false;
			this.sxshow = false;
		},
		newshopclick: function() {
			this.newshop = !this.newshop;
		},
		ontimeclick: function() {
			this.ontime = !this.ontime;
		},
		mokeysendclick: function() {
			this.mokeysend = !this.mokeysend;
		},
		list3qk: function() {
			this.newshop = false;
			this.ontime = false;
			this.mokeysend = false;
		},
		list3tj: function() {
			this.shop = [];
			this.shopstar = [];
			this.shopattr1 = [];
			this.shopattr2 = [];
			this.b2cFar = [];
			//根据距离筛选商家
			var _this = this;
			var juli;
			// 创建地址解析器实例
			var map = new BMap.Map("allmap");
			var myGeo = new BMap.Geocoder();
			// 将地址解析结果显示在地图上,并调整地图视野
			var query = new AV.Query('ELM_shop');
			if(this.newshop == true) {
				query.contains('attr3', 'true');
			}
			if(this.ontime == true) {
				query.contains('attr1', 'true');
			}
			if(this.mokeysend == true) {
				query.contains('attr2', 'true');
			}
			query.find().then(function(data) {
				console.log(data.length);
				for(var i = 0; i < data.length; i++) {
					//使用闭包将i存入
					(function(i) {
						myGeo.getPoint(data[i].attributes.adress, function(point) {
							if(point) {
								/*商家和我现在的距离*/
								juli = map.getDistance(_this.ownPoint, point).toFixed(2);
								if(juli < 50000) {
									_this.shop.push(data[i]);
									_this.shopstar.push(parseInt(data[i].attributes.grade));
									_this.shopattr1.push(eval(data[i].attributes.attr1));
									_this.shopattr2.push(eval(data[i].attributes.attr2));
									_this.b2cFar.push((juli / 1000).toFixed(2));
								}
							} else {
								alert("请您输入正确格式(省+市+区+具体位置)");
							}
						})
					})(i);
				}
			}, function(error) {
				// 异常处理
			});
			this.flshow = false;
			this.blackshow = false;
			this.pxshow = false;
			this.sxshow = false;
		},
		tzshop:function(id){
			location.href="../shopdata/shopdata.html?shopid="+seturl(id);
		}
	}
})